using System;
using System.Collections.Generic;
using System.Text;
using VisualEsse.DataEngine.Types;

namespace VisualEsse.DataEngine
{
    public class NcmlDataProvider : EsseResponseDataProvider
    {
        private GeoUniformGrid2d grid;
        private NcmlGeoGridDocument doc;
        private Dictionary<string, CdmVariable> parameters;

        public NcmlDataProvider(string path, string homeDirectory)
            : base(homeDirectory, false)
        {
            doc = new NcmlGeoGridDocument();
            doc.Load(path);
            Init(doc, homeDirectory);
        }

        public NcmlDataProvider(NcmlGeoGridDocument ncml, string homeDirectory)
            : base(homeDirectory, false)
        {
            this.doc = ncml;
            Init(ncml, homeDirectory);
        }

        private void Init(NcmlGeoGridDocument ncml, string homeDirectory)
        {
            InitMetadata(ncml, homeDirectory);
        }

        private void InitMetadata(NcmlGeoGridDocument ncml, string homeDirectory)
        {
            DateTime[] times = ncml.GetTimes();
            TemporalGrid temporal = new TemporalGrid(times[0], times[times.Length - 1], times[1] - times[0]);

            GeoUniformGrid2d geoGrid = ncml.GetGrid();
            grid = geoGrid;
            EsseMetadataProvider esseMetadata = EsseMetadataProvider.CreateEsseMetadataProvider(homeDirectory);
            GenericMetadataProvider gmp = new GenericMetadataProvider(homeDirectory, temporal, grid, esseMetadata);
            base.metadataProvider = gmp;

            parameters = new Dictionary<string, CdmVariable>();
            foreach (CdmVariable var in ncml.GetValuesVariables())
            {
                string name = var.Name;
                string vc = "";
                int i = name.IndexOf('_');
                if (i >= 0)
                {
                    vc = name.Substring(i + 1, name.Length - i - 1);
                    name = name.Substring(0, i);
                }
                EsseParameter ep = new EsseParameter(name, 0);
                EsseParameterDescription epd;

                if (esseMetadata.Exists(ep))
                {
                    gmp.AddParameter(epd = esseMetadata.GetDescription(ep));
                }
                else
                {
                    epd = new EsseParameterDescription(ep);
                    epd.Description = var.GetAttribure("long_name");
                    epd.Units = var.GetAttribure("units");
                    gmp.AddParameter(epd);
                }
                parameters.Add(epd.Name, var);
            }
        }


        public override IGrid2dDataSource CreateDataSource(EsseParameterDescription parameter, DateTime when)
        {
            string pname = parameters[parameter.Name].Name;
            ScalarArray2d data = doc.GetData(pname, when);
            GeoUniformScalarField2d sf = new GeoUniformScalarField2d(grid, data);

            ScalarFieldDataSource sfDataSource = new ScalarFieldDataSource(sf);
            return sfDataSource;
        }


    }
}
